#python wrapper for wget utility 
#This module contributes under term of GNU/GPLv2   
#Copy of text GNU/GPLv2 you can find in folder with source
#Debug Version unstable 

import subprocess,socket
key=["wget"];#this is global list for paramaters of call wget 
def SendToCLI():#release+
 
 print key#test output 
 retcode=subprocess.call(key);
 return retcode 

     
def download(url,path=' '):
   """This function provide basic  download,you can send path for saving to wget  
   """
   if path==' ':
      key.append(url)
      retcode=SendToCLI()
      return retcode
   else:
       path='-P '+path 
       key.append(path) 
       key.append(url)   
       retcode=SendToCLI() 
       return retcode  
def download_in_time(time):
 """Function for download in specific time"""
 
 print key
def download_alert(adress,alert_ext):
 """Function scan adress of download and if find extension similar to extension 
    in illegal extensions list,return  -1"""
 return 0
def download_site (adress,r=-1,path=''):#function realise+
 """full download of site,this function generate
 offline version of target site. You can get function depth of recursion """
 if path=='':
   
  if r==-1:
     key.append("-r") 
     key.append("-k")
     
     print key
     
  else:
    key.append("-r")
    key.append("-l"+str(r))
    key.append("-k")
    key.append(adress) 
    print key
    
 else:
    if r==-1:
     key.append("-r") 
     key.append("-k")
    else:
     key.append("-r")
     key.append("-l"+str(r))
     key.append("-k")
     
 return 0
          
def check_cli(raw_prompt ):
 """check string to CLI from user"""
 #Under constructiom
 #in future add regular_exp check 
 return 0
def  write_in_log():
    """send output wget and/or errors of user in log"""
     
    return 0
def take_from_file (name):#realise+ 
 """This function take list and generates file with
 list of target URL""" 
 key.append('-i')
 key.append(name)
 return 0
def write_file_wget(listdownload,path):#realise
    f=open(path,'w')
    for i in listdownload:
     i=i+'\n'
     f.write(i)
    f.close()
    return 0;
def show_output():
    """show output(send to GUI element of Pywget)"""
    return 0
def download_with_pass(name,passw,adress):
    if 'http' in adress: 
      key.append("--http-user="+name) 
      key.append("--http-password="+passw)
    elif 'ftp' in adress:
     key.append("--ftp-user="+name) 
     key.append("--ftpp-password="+passw)
    else:
      return -1 
  
    return 0
def IsOnline():#under construction.Task of this function start pyWget on  up of network interface 
 """check:is internet available  """
 
def IsAdressAvailable(host):#realise+

 try: 
       i= socket.gethostbyname(host)
       return 0
 except:
          return -1  



if download_with_pass('foo1','foo','http:\\www.google.ru')==0:
  download('http://www.google.ru')
  if  SendToCLI()==0:
    print 'ok'
  else:
    print 'download was failed'
else:
    print 'Incorrect adress'
         
    
   

